package com.titar.robot.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.titar.robot.R;


/**
 * Created by window on 2017/6/5.
 */

public class IconTextView extends LinearLayout {
    public static final String DEFAULT_NAMESPACE = "http://schemas.android.com/apk/res/android";

    private static final int DEFAULT_ID = - 1;

    private Context mContext;

    private ImageView mIvIcon;
    private TextView mTextView;

    public IconTextView(Context context) {
        this(context, null);
    }

    public IconTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.view_icon_text_view, this, true);

        mIvIcon = (ImageView) findViewById(R.id.iv_icon);
        mTextView = (TextView) findViewById(R.id.ed_text);

        obtainAttributes(attrs);
    }

    private void obtainAttributes(AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.IconEditText);

        int iconId = a.getResourceId(R.styleable.IconEditText_textIcon, DEFAULT_ID);
        boolean password = a.getBoolean(R.styleable.IconEditText_password, false);
        String text = a.getString(R.styleable.IconEditText_text);
        String hint = a.getString(R.styleable.IconEditText_hint);
        a.recycle();

        if(iconId != DEFAULT_ID)
            mIvIcon.setImageResource(iconId);
        if(password)
            // 显示为密码
            mTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        mTextView.setText(text);
        mTextView.setHint(hint);
    }

    public void setImageResource(int resId) {
        mIvIcon.setImageResource(resId);
    }

    public void setText(CharSequence text) {
        mTextView.setText(text);
    }

    public CharSequence getText() {
        return mTextView.getText();
    }

    public void setHint(CharSequence text) {
        mTextView.setHint(text);
    }

}

